package com.ca.project.Server.Modules.UserWeb.Controller;

import com.ca.project.Server.Modules.UserWeb.Context.WebUserLoginContext;
import com.ca.project.Server.Modules.UserWeb.Context.WebUserQuestionContext;
import com.ca.project.Server.Modules.UserWeb.Context.WebUserRegisterContext;
import com.ca.project.Server.Modules.UserWeb.Converter.UserWebConverter;
import com.ca.project.Server.Modules.UserWeb.PO.WebUserLoginPO;
import com.ca.project.Server.Modules.UserWeb.PO.WebUserQuestionPO;
import com.ca.project.Server.Modules.UserWeb.PO.WebUserRegisterPO;
import com.ca.project.Server.Modules.UserWeb.Service.UserWebService;
import com.ca.project.core.Respond.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

/**
 * @PACKAGE_NAME: com.ca.project.Server.Modules.UserWeb.Controller
 * @NAME: UserWebController
 * @USER: ASUS
 * @DATE: 30/12/2024
 * @TIME: 下午6:21
 **/
@RestController
@RequestMapping("/user_web")
@Api(tags = "web端用户模块")
public class UserWebController {

    @Autowired
    private UserWebService userWebService;

    @Autowired
    private UserWebConverter userWebConverter;

    @ApiOperation(
            value = "Web端用户注册接口",
            notes = "该接口提供了web端用户注册的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/register")
    public R Register(@Validated @RequestBody WebUserRegisterPO webUserRegisterPO) {
        WebUserRegisterContext webUserRegisterContext =
                userWebConverter.WebUserRegisterPO2WebUserRegisterContext(webUserRegisterPO);
        Map<String, Object> data = userWebService.register(webUserRegisterContext);
        return R.data(data);
    }

    @ApiOperation(
            value = "Web端用户填写密保问题答案接口",
            notes = "该接口提供了web端用户填写密保问题答案的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/question")
    public R Question(@Validated @RequestBody WebUserQuestionPO webUserQuestionPO) {
        WebUserQuestionContext webUserQuestionContext =
                userWebConverter.WebUserQuestionPO2WebUserQuestionContext(webUserQuestionPO);
        userWebService.updateQuestion(webUserQuestionContext);
        return R.success();
    }

    @ApiOperation(
            value = "Web端用户登录接口",
            notes = "该接口提供了web端用户登录的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/login")
    public R Login(@Validated @RequestBody WebUserLoginPO webUserLoginPO) {
        WebUserLoginContext webUserLoginContext =
                userWebConverter.WebUserLoginPO2WebUserLoginContext(webUserLoginPO);
        userWebService.login(webUserLoginContext);
        return R.success();
    }

}
